//
//  XQHomeFirstTableViewCell.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/25.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit


class fistCollectViewCell: UICollectionViewCell {
    var icon = UIImageView()
    var label = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func InitUI(){
        icon = UIImageView.init(frame: CGRect.init(x: self.frame.width/2-25, y: 15, width: 50, height: 50))
        self.contentView .addSubview(icon)
        
        label = UILabel.init(frame: CGRect.init(x: 0, y: self.frame.size.height-20, width: self.frame.size.width, height: 20))
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        self.contentView .addSubview(label)
    }
    
}



class XQHomeFirstTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    public var homeself = XQHomeViewController()
    var collectionView: UICollectionView!
    
    private  let fistCollectViewCellID = "fistCollectViewCellID"

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func InitUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: SCREEN_WIDTH/4, height: SCREEN_WIDTH/4)
       
         self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH/2), collectionViewLayout: layout)
         self.collectionView.delegate = self;
         self.collectionView.dataSource = self;
         self.collectionView.showsVerticalScrollIndicator = false
         self.collectionView.showsHorizontalScrollIndicator = false
         self.collectionView.bounces = false
         self.collectionView.backgroundColor = UIColor.white
        self.contentView .addSubview( self.collectionView)
        
        self.collectionView.register(fistCollectViewCell.self, forCellWithReuseIdentifier: fistCollectViewCellID)

    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: fistCollectViewCellID, for: indexPath) as!fistCollectViewCell
        switch indexPath.row {
        case 0:
            cell.icon.image = UIImage.init(named: "course1")
            cell.label.text = "课程"
            break
        case 1:
            cell.icon.image = UIImage.init(named: "position1")
            cell.label.text = "职位"
            break
        case 2:
            cell.icon.image = UIImage.init(named: "wenda1")
            cell.label.text = "精英问答"
            break
        case 3:
            cell.icon.image = UIImage.init(named: "item1")
            cell.label.text = "众包项目"
            break
        case 4:
            cell.icon.image = UIImage.init(named: "hot1")
            cell.label.text = "热门精华"
            break
        case 5:
            cell.icon.image = UIImage.init(named: "newguid1")
            cell.label.text = "新手指南"
            break
        case 6:
            cell.icon.image = UIImage.init(named: "huodong1")
            cell.label.text = "活动广场"
            break
        case 7:
            cell.icon.image = UIImage.init(named: "huawei1")
            cell.label.text = "华为专区"
            break
      
        default:
            break
        }
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            homeself.tabBarController?.selectedIndex = 1
            break
        case 1:
            homeself.tabBarController?.selectedIndex = 2
            break
        case 2:
            break
        case 3:
            break
        case 4:
            break
        case 5:
            break
        case 6:
            break
        case 7:
            break
        default:
            break
        }
    }
    
    
}
